package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.PageBean;
import com.wzq.dao.pojo.Place;
import com.wzq.dao.pojo.Project;
import com.wzq.dao.pojo.R;
import com.wzq.service.IPlaceService;
import com.wzq.service.IProjectService;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台管理-用户Controller控制器
 */
@RestController
@RequestMapping("/admin/devices")
public class AdminDevicesController {

    @Autowired
    IProjectService iProjectService;

    /**
     * 根据条件分页查询用户信息
     * @param pageBean
     * @return
     */
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        String query=pageBean.getQuery().trim();
        Page<Project> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Project> pageResult = iProjectService.page(page, new QueryWrapper<Project>()
                .like(StringUtil.isNotEmpty(query), "title", query)
                .in("type",7,8,9));
        Map<String,Object> map=new HashMap<>();

//        System.out.println("pzmpppppppppppppppp"+pageResult.getRecords());
//        Map<String,Object> map1=new HashMap<>();
//        List<Project> list=new ArrayList<>();
//        for (Project record : pageResult.getRecords()) {
//            System.out.println(record+"pjrrrrrrrrrrrrrrrrrr");
//            System.out.println(record.getType());
//            if (record.getType()==7||record.getType()==9||record.getType()==9)
//            {
//                list.add(record);
//            }
//        }
        map.put("DevicesList",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id")Integer id){
        iProjectService.removeById(id);
        return R.ok();
    }

    @RequestMapping("/listAll")
    public R listAll(){
        Map<String,Object> map=new HashMap<>();
        map.put("DevicesList",iProjectService.list());
        return R.ok(map);
    }

    @RequestMapping("/save")        //添加修改
    public R save(@RequestBody Project project){
        if(project.getId()==null || project.getId()==-1){
            iProjectService.save(project);
        }else{
            iProjectService.saveOrUpdate(project);
        }
        return R.ok();
    }

    @GetMapping("/{id}")            //单个回显
    public R findById(@PathVariable(value = "id")Integer id){
        Project project = iProjectService.getById(id);
        //        获得用户名称和项目名称
        Map<String,Object> map=new HashMap<>();
        map.put("project",project);
        return R.ok(map);
    }

}
